package com.bobi.tfca.controller.article;

import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.ArticleVO;
import com.bobi.tfca.service.ArticleService;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李文发
 * @date 2024/4/15
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章接口")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "获得全部文化展示文章")
    @GetMapping("/findAllCultures")
    public R<List<ArticleVO>> findAllCultures() {
        List<ArticleVO> list = articleService.findAllCultures();
        return R.data(list);
    }

    @ApiOperation(value = "查询文章详情")
    @GetMapping("/getArticleDetails/{articleId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleId", value = "文章id")})
    public R<ArticleVO> getArticleDetails(@CurrentUser MemberEntity member, @PathVariable("articleId") Integer articleId) {
        ArticleVO activity = articleService.getArticleDetails(articleId, member);
        return R.data(activity);
    }

    @ApiOperation(value = "发布文章")
    @PostMapping("/publishArticle")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleVO", value = "文章内容"),
            @ApiImplicitParam(name = "member", value = "当前用户")})
    public R<Integer> publishArticle(@CurrentUser MemberEntity member, @RequestBody ArticleVO articleVO) {
        Integer activityId = articleService.publishArticle(member, articleVO);
        return R.data(activityId);
    }

    @ApiOperation(value = "删除文章")
    @PostMapping("/deleteById/{articleId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleId", value = "文章id")})
    public R<String> deleteArticle(@PathVariable("articleId") Integer articleId) {
        Boolean b = articleService.deleteArticle(articleId);
        return R.data("删除成功");
    }

    @ApiOperation(value = "点赞文章")
    @PostMapping("/likeArticle/{articleId}")
    @ApiImplicitParams({@ApiImplicitParam(name = "articleId", value = "文章id"),
            @ApiImplicitParam(name = "isLiked", value = "是否点赞")})
    public R<String> likeArticle(@CurrentUser MemberEntity member, @RequestParam("isLiked") Boolean isLiked, @PathVariable("articleId") Integer articleId) {
        Boolean b = articleService.likeArticle(member, isLiked, articleId);
        return R.data("操作成功");
    }

}
